package cn.boy.gui.student;

import cn.boy.adapter.GridViewAdapter;
import cn.boy.gui.AbortActivity;
import cn.boy.gui.HelpActivity;
import cn.boy.gui.LoginActivity;
import cn.boy.gui.adminstrator.AdminHistoryActivity;
import cn.boy.gui.adminstrator.AdminInfoActivity;
import cn.boy.gui.adminstrator.AdminMarkActivity;
import cn.boy.gui.adminstrator.AdminSettingActivtiy;
import cn.boy.gui.adminstrator.DormitoryMemberActivity;
import cn.boy.gui.adminstrator.RankActivity;
import cn.boy.home.HomeApplication;
import cn.boy.home.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.AdapterView.OnItemClickListener;

public class StudentMainAcitivity extends Activity {
	
	GridView gridView;
	
	StudentMainAcitivity instance;
	
	HomeApplication app;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_student);
		
		findViews();
		
		instance = this;
		
		app = (HomeApplication) getApplicationContext();
		
		initGridView();
	}

	private void initGridView() {
		String[] names = { "个人信息","得分","排行榜", "宿舍成员", "历史", "设置", "注销",
				"帮助", "关于", "退出" };
		int[] images = { R.drawable.main_information ,R.drawable.main_messageboard,
				R.drawable.main_search,R.drawable.main_healthrecord, R.drawable.main_friend,
				R.drawable.main_settings, R.drawable.main_logout,
				R.drawable.main_help, R.drawable.main_about,
				R.drawable.main_exit };
		//获取屏幕大小
		DisplayMetrics  dm = new DisplayMetrics(); 
		getWindowManager().getDefaultDisplay().getMetrics(dm); 
		int view_h = dm.heightPixels; 
		gridView.setGravity(Gravity.CENTER);
		if(view_h==320){
			gridView.setVerticalSpacing(20);// 垂直间隔
		}else if(view_h==480){
			gridView.setVerticalSpacing(40);// 垂直间隔
		}else if(view_h==640){
			gridView.setVerticalSpacing(60);// 垂直间隔
		}else{
			gridView.setVerticalSpacing(80);// 垂直间隔
		}
		gridView.setHorizontalSpacing(10);// 水平间隔
		
		GridViewAdapter adapter = new GridViewAdapter(this);
		adapter.setImages(images);
		adapter.setNames(names);
		gridView.setAdapter(adapter);
		
		setOnGridViewClick();
	}

	private void setOnGridViewClick() {
		gridView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				switch(position){
					case 0:{ // information
						Intent intent = new Intent(instance, StudentInfoActivity.class);
						app.setFlag(true);
						startActivity(intent);
						break;
					}
					case 1:{	// mark of dormitory
						Intent intent = new Intent(instance, StudentMarkActivity.class);
						startActivity(intent);
						break;
					}
					case 2: {// ranking list
						Intent intent = new Intent(instance, RankActivity.class);
						startActivity(intent);
						break;
					}
					case 3:{ // the member of one dormitory
						Intent intent = new Intent(instance, StudentMemberActivity.class);
						startActivity(intent);
						break;
					}
					case 4:{// the history of mark of  the dormitory
						Intent intent = new Intent(instance, StudentHistoryActivity.class);
						startActivity(intent);
						break;
					}
					case 5:{// setting
						Intent intent = new Intent(instance, AdminSettingActivtiy.class);
						startActivity(intent);
						finish();
						break;
					}
					case 6:{//cancle
						Intent intent = new Intent(instance, LoginActivity.class);
						intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
						startActivity(intent);
						finish();
						break;
					}
					case 7: { // help
						Intent intent = new Intent(instance, HelpActivity.class);
						startActivity(intent);
						break;
					}
					case 8:{  // abort
						Intent intent = new Intent(instance, AbortActivity.class);
						startActivity(intent);
						break;
					}
					case 9:{// exit
						android.os.Process.killProcess(android.os.Process.myPid());
						break;
					}
				
				}
				
			}
		});
	}

	private void findViews() {
		gridView = (GridView)findViewById(R.id.gv_main);
	}

}
